<?php
/**
*
* @package phpBB
* @version $Id: functions_menu.php 0 8 jan 2009 19:22:33 Alexander de Jong $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @package phpBB
*/
class  menu_handler
{
	var $menus = array();
	
	function menu_handler()
	{
		$this->load_items();
	}
	
	/**
	* bool add_item
	* 
	* Adds a menu item 
	* 
	* @param array $items
	* @param bool $multi
	* @access public
	* @return bool $success
	*/
	function add_item($items, $multi = false)
	{
		global $db;
		
		if($multi === true)
		{
			foreach($items as $item)
			{
				$this->add_item($item, false);
			}
		}
		
		$sql = "INSERT INTO " . MENU_TABLE;
		$sql .= $db->sql_build_array("INSERT", $items);
		
		if($db->sql_query($sql))
		{
			$this->reload_items();
			return true;
		}
		
		return false;
	}
	
	function update_item($menu_id, $items, $multi = false)
	{
		global $db;
		
		if($multi === true)
		{
			foreach($items as $item)
			{
				$this->update_item($item, false);
			}
		}
		
		$sql = "UPDATE " . MENU_TABLE;
		$sql .= " SET " . $db->sql_build_array('UPDATE', $items);
		$sql .= " WHERE menu_id = " . $menu_id;
		
		$db->sql_query($sql);
		
		if($db->sql_affectedrows() > 0)
		{
			$this->reload_items();
			return true;
		}
		
		return false;
	}
	
	function remove_item($menu_id)
	{
		global $db;
		
		$place = $this->get_order_from_item($menu_id);
		$hplace = $this->get_highest_position();
		
		if($place == $hplace)
		{
			$sql = "DELETE FROM " . MENU_TABLE;
			$sql .= " WHERE menu_id = " . $menu_id;
			$db->sql_query($sql);
			return;
		}
		
		$sql = "DELETE FROM " . MENU_TABLE;
		$sql .= " WHERE menu_id = " . $menu_id;
		$db->sql_query($sql);
		
		for($i = $place+1; $i <= $hplace; $i++)
		{
			$_menu_id = $this->get_item_from_order($i);
			$order = $i-1;
			$sql = "UPDATE " . MENU_TABLE . " SET menu_order = " . $order . " WHERE menu_id = " . $_menu_id;	
			$db->sql_query($sql);	
		}
		
		$this->reload_items();
	}
	
	function push_item_up($menu_id) 
	{
		global $db;
		
		$place = $this->get_order_from_item($menu_id);
		$update_place = $place-1;
		
		// get the place above the current menu item.
		$above_item = $this->get_item_from_order($update_place);
		
		// update the current menu
		$sql = "UPDATE " . MENU_TABLE;
		$sql .= " SET menu_order = 0";
		$sql .= " WHERE menu_id = " . $menu_id;
		$db->sql_query($sql);
		
		$sql = "UPDATE " . MENU_TABLE;
		$sql .= " SET menu_order = " . $place;
		$sql .= " WHERE menu_id = " . $above_item;
		$db->sql_query($sql);
		
		$sql = "UPDATE " . MENU_TABLE;
		$sql .= " SET menu_order = " . $update_place;
		$sql .= " WHERE menu_id = " . $menu_id;
		$db->sql_query($sql);
		
		$this->reload_items();
	}
	
	function push_item_down($menu_id) 
	{ 
		global $db;
		
		// get the current place
		$place = $this->get_order_from_item($menu_id);
		$update_place = $place+1;
		
		// get the place above the current menu item.
		$above_item = $this->get_item_from_order($update_place);
		
		// update the current menu
		$sql = "UPDATE " . MENU_TABLE;
		$sql .= " SET menu_order = 0";
		$sql .= " WHERE menu_id = " . $menu_id;
		$db->sql_query($sql);
		
		$sql = "UPDATE " . MENU_TABLE;
		$sql .= " SET menu_order = " . $place;
		$sql .= " WHERE menu_id = " . $above_item;
		$db->sql_query($sql);
		
		$sql = "UPDATE " . MENU_TABLE;
		$sql .= " SET menu_order = " . $update_place;
		$sql .= " WHERE menu_id = " . $menu_id;
		$db->sql_query($sql);
		
		$this->reload_items();
	}
	
	function load_items()
	{
		global $db, $phpbb_root_path, $user;
		
		$sql = "SELECT * FROM " . MENU_TABLE;
		$sql .= " ORDER BY menu_order ASC";
		
		$result = $db->sql_query($sql);
		
		$row = array();
		
		while($row = $db->sql_fetchrow($result))
		{
			// create the link
			$link = explode('?', $row['menu_link']);
			
			if(!empty($link[1]))
			{
				$args = explode('&', $link[1]);
				
				$page = $link[0] . '?' . $args[0];
				
				$sub_args = "";
				if(!empty($args[1]))
				{
					for($i = 1; $i <= count($args)-1; $i++)
					{
						$sub_args .= '&' . $args[$i];
					}
				}
			}
			
			$this->menus[] = array(
				'name'			=> (!empty($user->lang[$row['menu_name']])) ? $user->lang[$row['menu_name']] : $row['menu_name'],
				'name_var'		=> $row['menu_name'],
				'link'			=> (!empty($link[1])) ? append_sid("{$phpbb_root_path}$page", $sub_args) : append_sid("{$phpbb_root_path}{$row['menu_link']}"),
				'link_db'		=> $row['menu_link'],
				'id'			=> $row['menu_id'],
				'order'			=> $row['menu_order'],
				'not_created'	=> ($row['menu_not_created'] == 1) ? true : false,
				'disabled'		=> ($row['menu_disabled'] == 1) ? true : false,
				'outside_link'	=> ($row['menu_outside_link'] == 1) ? true : false, // !NEW need to be implemented. Not in use now.
				'active_check'	=> $row['menu_active_check'],
			);
		}
	}
	
	function reload_items()
	{
		unset($this->menus);
		
		$this->load_items();
	}
	
	function _debug_print($array)
	{
		print "<pre>" . print_r($array, true) . "</pre>";
		// die();
	}
	
	function check_active($menu_id)
	{
		global $db;
		
		$sql = "SELECT * FROM " . MENU_TABLE . " WHERE menu_id = " . $menu_id;
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult();
		
		$active_check = json_decode($row['menu_active_check'], true);
		
		// $this->_debug_print($active_check);
		
		$current_file = basename($_SERVER['SCRIPT_FILENAME']);
		$current_file = explode('?', $current_file);
		$current_file = $current_file[0];
		
		//print $current_file;
		
		foreach($active_check as $var => $val)
		{
			if($val == 'basename')
			{
				if($var == $current_file)
				{
					return true;
				}
			}
			elseif($val == 'head') 
			{
				$t = explode('=', $var);
				
				if(isset($_GET[$t[0]]) && $_GET[$t[0]] == $t[1])
				{
					return true;
				}
			}
			elseif($val == 'get')
			{
				$t = explode('=', $var);
				
				if(isset($_GET[$t[0]]) && $_GET[$t[0]] == $t[1])
				{
					return true;
				}
			}
			else
			{
				return false;
			}
		}
		
		return false;
	}
	
	function exists($menu_name)
	{
		global $db;
		
		$sql = "SELECT * FROM " . MENU_TABLE . " WHERE menu_name = '" . $menu_name . "'";
		$result = $db->sql_query($sql);
		$db->sql_freeresult();
		
		if($db->sql_affectedrows() > 0)
		{
			return true;
		}
		
		return false;
	}
	
	/**
	* Implemented functions from the old menu class. This has NOT the new syntax.
	*/
	
	function get_item_from_link($name)
	{
		global $db;
		
		$sql = "SELECT * 
				FROM " . MENU_TABLE . " 
				WHERE menu_link = '" . $name . "'";
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult();	

		return $row['menu_id'];
	}
	
	function get_item_from_order($order) 
	{
		global $db;
		
		$sql = "SELECT *";
		$sql .= " FROM " . MENU_TABLE;
		$sql .= " WHERE menu_order = " . $order;
		
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult();
		
		return $row['menu_id'];
	}
	
	function get_order_from_item($menu_id)
	{
		global $db;
		
		$sql = "SELECT *
				FROM " . MENU_TABLE . "
				WHERE menu_id = '" . $menu_id . "'";
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult();
		
		return $row['menu_order'];
	}
	
	function get_name_from_item($menu_id)
	{
		global $db;
		
		$sql = "SELECT *
				FROM " . MENU_TABLE . "
				WHERE menu_id = '" . $menu_id . "'";
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult();
		
		return $row['menu_name'];
	}
	
	function get_highest_position()
	{
		global $db;
		
		$sql = "SELECT max(menu_order) FROM " . MENU_TABLE;
		$res = $db->sql_query($sql);
		$row = $db->sql_fetchrow($res);
		$db->sql_freeresult();
		
		return $row['max(menu_order)'];
	}
}

?>